//
//  Gee_loadingAlert.m
//  GeelyApp
//
//  Created by LJQ on 2021/3/18.
//  Copyright © 2021 Geely. All rights reserved.
//

#import "LoadingView_HYC_Neusoft.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import <Masonry/Masonry.h>
@interface LoadingView_HYC_Neusoft()
{
    dispatch_source_t _timer;
}


@property (nonatomic ,strong) UIView *alertView;
@property (nonatomic ,strong) UIImageView *loadingView;
@property (nonatomic ,strong) UILabel *msgLabel;

@property (nonatomic ,strong) UIImageView *dataLoadingView;
@property (nonatomic ,strong) UILabel *dataMsgLabel;
@property (nonatomic ,strong) UIView *msgAlertView;
@property (nonatomic,assign)int count;


@end


@implementation LoadingView_HYC_Neusoft


+(SVProgressHUD *)hud_share {
    static SVProgressHUD *share = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[SVProgressHUD alloc] init];
    });
    return share;
}


+(instancetype)share {
    static LoadingView_HYC_Neusoft *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LoadingView_HYC_Neusoft alloc] init];
    });
    return manager;
}



-(void)gee_msgLoadingMsg:(NSString *)msg {
    //gee_loading
//
//
    __weak typeof(self) weakSelf = self;
    [self.window addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(GEE_KEYWINDOW);
    }];

    self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.6];

    [self addSubview:self.msgAlertView];
    [self.msgAlertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.width.mas_equalTo(ZLVH_GEE_DISTANCE(124));
        make.height.mas_equalTo(ZLVH_GEE_DISTANCE(124));
        make.centerY.equalTo(weakSelf.mas_centerY);
    }];
    self.msgAlertView.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.8];
    [self.msgAlertView GEESetCorRadius:4];


    [self.msgAlertView addSubview:self.dataLoadingView];
    [self.dataLoadingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.msgAlertView);
        make.top.equalTo(weakSelf.msgAlertView).offset(ZLVH_GEE_DISTANCE(30));
        make.height.mas_equalTo(ZLVH_GEE_DISTANCE(36));
        make.width.mas_equalTo(ZLVH_GEE_DISTANCE(36));
    }];
    self.dataLoadingView.image = @"ZLVH_Loading_HYC".image;

    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 1;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 10000;
    [rotationAnimation setRemovedOnCompletion:NO];
    [self.dataLoadingView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    

    [self.msgAlertView addSubview:self.msgLabel];
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.msgAlertView.mas_centerX);
        make.top.equalTo(weakSelf.dataLoadingView.mas_bottom).offset(ZLVH_GEE_DISTANCE(10));
    }];
    self.msgLabel.font = ZLVH_GEE_FONT_Regular(13);
    self.msgLabel.textColor = UIColor.color_FFFFFF;
    self.msgLabel.text = msg;
    
//    [self discountTimer];
}



-(void)showDisCountTime:(NSString *)timeStr {
    ZLVH_GEE_WeakSelf(weakSelf);
    
    [GEE_KEYWINDOW addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(GEE_KEYWINDOW);
    }];
    
    self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.6];
    
    
    [self addSubview:self.alertView];
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.width.mas_equalTo(ZLVH_GEE_DISTANCE(124));
        make.height.mas_equalTo(ZLVH_GEE_DISTANCE(124));
        make.centerY.equalTo(weakSelf.mas_centerY);
    }];
    self.alertView.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.8];
    [self.alertView GEESetCorRadius:4];
    
    
    [self.alertView addSubview:self.msgLabel];
    [self.msgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf.alertView);
    }];
    self.msgLabel.font = ZLVH_GEE_FONT_Semibold(40);
    self.msgLabel.textColor = UIColor.color_FFFFFF;
    self.msgLabel.text = timeStr;
    self.msgLabel.textAlignment = NSTextAlignmentCenter;
    
}

-(UIView *)msgAlertView {
    if (_msgAlertView == nil) {
        _msgAlertView = UIView.alloc.init;
    }
    return _msgAlertView;
}




-(UIImageView *)dataLoadingView {
    if (_dataLoadingView == nil) {
        _dataLoadingView = UIImageView.alloc.init;
    }
    return _dataLoadingView;
}


-(UILabel *)msgLabel {
    if (_msgLabel == nil) {
        _msgLabel = UILabel.alloc.init;
    }
    return _msgLabel;
}



-(UIView *)alertView {
    if (_alertView == nil) {
        _alertView = UIView.alloc.init;
    }
    return _alertView;
}

-(UIImageView *)loadingView {
    if (_loadingView == nil) {
        _loadingView = UIImageView.alloc.init;
    }
    return _loadingView;
}




-(void)dismissLoading {
//    if (self.count != 0) {
//        self.count = -1;
//    }
    
    [self.loadingView stopAnimating];
    for (UIView *item in self.subviews) {
        [item removeFromSuperview];
    }
    [self removeFromSuperview];
}


#pragma mark - 超时倒计时
//-(void)discountTimer {
//    ZLVH_GEE_WeakSelf(weakSelf);
//    if (_timer == nil) {
//        self.count = 45;
//        _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));
//        dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
//        dispatch_source_set_event_handler(_timer, ^{
//            dispatch_async(dispatch_get_main_queue(), ^{
//                if(weakSelf.count == 0){
//                    //倒计时结束
//                    NSLog(@"_timer ________ ///%@",_timer);
//                    [weakSelf dismissLoading];
//                    dispatch_source_cancel(_timer);
//                    _timer = nil;
//                }else if (weakSelf.count < 0) {
//                    [weakSelf dismissLoading];
//                    @try {
//                        NSLog(@"_timer ________ ///%@",_timer);
//                        [weakSelf dismissLoading];
//                        dispatch_source_cancel(_timer);
//                        _timer = nil;
//                    } @catch (NSException *exception) {
//                        NSLog(@"%@",exception);
//                    } @finally {
//                        NSLog(@"倒计时结束");
//                    }
//
//                }else{
//                    //倒计时进行时每次执行
//
//                    weakSelf.count--;
//                    LWLog(@"%d",weakSelf.count);
//                }
//            });
//        });
//        dispatch_resume(_timer);
//    }else {
//        HDNormalLog(@"_timer = nil");
////        [self dismissLoading];
//    }
//
//}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
